package com.itheima.controller;

import com.itheima.anno.OperationLog;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hu
 * @date 2025/1/29
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

//    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result select() {
        log.info("查询全部部门");
        List<Dept> deptList = deptService.listAll();
        return Result.success(deptList);
    }

    @OperationLog
    @DeleteMapping
    public Result delete(Integer id) {
        deptService.deleteById(id);
        return Result.success();
    }

//    @DeleteMapping
//    public Result delete(@RequestParam("id") Integer deptId) {
//        deptService.deleteById(deptId);
//        return Result.success();
//    }


//    @PostMapping
//    public Result add(@RequestBody String name ){
//        System.out.println(name);
//        return Result.success();
//    }
    @OperationLog
    @PostMapping
    public Result add(@RequestBody Dept dept){
        deptService.addDept(dept);
        return Result.success();
    }

    @OperationLog
    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.updateDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectDeptById(@PathVariable Integer id){
        Dept dept = deptService.selectDeptById(id);
        return Result.success(dept);
    }

//    @GetMapping("/{id}")
//    public Result selectDeptById(@PathVariable("id") Integer deptId){
//        Dept dept = deptService.selectDeptById(deptId);
//        return Result.success(dept);
//    }
}
